//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BatesDetJumpModel.h"
using namespace Cephei::QL::Models::Equity;
#include <gen/QL/Processes/HestonProcess.h>
#include <gen/QL/Models/CalibrationHelper.h>
#include <gen/QL/Math/Optimization/OptimizationMethod.h>
#include <gen/QL/Math/Optimization/EndCriteria.h>
#include <gen/QL/Math/Optimization/Constraint.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Models/Equity/BatesModel.h>
using namespace Cephei::QL::Processes;
using namespace Cephei::QL::Models;
using namespace Cephei::QL::Math::Optimization;
using namespace Cephei::QL::Math;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Equity::CBatesDetJumpModel::CBatesDetJumpModel (Cephei::QL::Processes::IHestonProcess^ process, Microsoft::FSharp::Core::FSharpOption<Double>^ lambda, Microsoft::FSharp::Core::FSharpOption<Double>^ nu, Microsoft::FSharp::Core::FSharpOption<Double>^ delta, Microsoft::FSharp::Core::FSharpOption<Double>^ kappaLambda, Microsoft::FSharp::Core::FSharpOption<Double>^ thetaLambda) : CBatesModel(CBatesDetJumpModel::typeid)
{
    CHestonProcess^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phBatesDetJumpModel = NULL;
#endif
        _Cprocess = safe_cast<CHestonProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::HestonProcess>& _process = static_cast<boost::shared_ptr<QuantLib::HestonProcess>&> (_Cprocess->GetShared ()); 
        QuantLib::Real _lambda = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (lambda) ? (QuantLib::Real)ValueHelper::Convert (lambda->Value) : 0.1); //4
        QuantLib::Real _nu = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (nu) ? (QuantLib::Real)ValueHelper::Convert (nu->Value) : 0.0); //4
        QuantLib::Real _delta = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (delta) ? (QuantLib::Real)ValueHelper::Convert (delta->Value) : 0.1); //4
        QuantLib::Real _kappaLambda = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (kappaLambda) ? (QuantLib::Real)ValueHelper::Convert (kappaLambda->Value) : 1.0); //4
        QuantLib::Real _thetaLambda = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (thetaLambda) ? (QuantLib::Real)ValueHelper::Convert (thetaLambda->Value) : 0.1); //4
        _ppBatesDetJumpModel = new boost::shared_ptr<QuantLib::BatesDetJumpModel> (new QuantLib::BatesDetJumpModel ( _process,  _lambda,  _nu,  _delta,  _kappaLambda,  _thetaLambda ));
        SetBatesModel (boost::dynamic_pointer_cast<QuantLib::BatesModel> (*_ppBatesDetJumpModel));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Models::Equity::CBatesDetJumpModel::CBatesDetJumpModel (boost::shared_ptr<QuantLib::BatesDetJumpModel>& childNative, Object^ owner) : CBatesModel(CBatesDetJumpModel::typeid)
{
#ifdef HANDLE
	_phBatesDetJumpModel = NULL;
#endif
	_ppBatesDetJumpModel = &childNative;
    _ppBatesModel = new boost::shared_ptr<QuantLib::BatesModel> (boost::dynamic_pointer_cast<QuantLib::BatesModel> (*_ppBatesDetJumpModel));
}
Cephei::QL::Models::Equity::CBatesDetJumpModel::CBatesDetJumpModel (QuantLib::BatesDetJumpModel& childNative, Object^ owner) : CBatesModel(CBatesDetJumpModel::typeid)
{
#ifdef HANDLE
	_phBatesDetJumpModel = NULL;
#endif
	_ppBatesDetJumpModel = new boost::shared_ptr<QuantLib::BatesDetJumpModel> (&childNative);
    _ppBatesModel = new boost::shared_ptr<QuantLib::BatesModel> (boost::dynamic_pointer_cast<QuantLib::BatesModel> (*_ppBatesDetJumpModel));
    _BatesDetJumpModelOwner = owner;
    _BatesModelOwner = owner;
}

Cephei::QL::Models::Equity::CBatesDetJumpModel::CBatesDetJumpModel (CBatesDetJumpModel^ copy) : CBatesModel(CBatesDetJumpModel::typeid)
{
#ifdef HANDLE
	_phBatesDetJumpModel = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBatesDetJumpModel = new boost::shared_ptr<QuantLib::BatesDetJumpModel> (copy->GetShared());
        _ppBatesModel = new boost::shared_ptr<QuantLib::BatesModel> (boost::dynamic_pointer_cast<QuantLib::BatesModel> (*_ppBatesDetJumpModel));
    }
}
Cephei::QL::Models::Equity::CBatesDetJumpModel::CBatesDetJumpModel (System::Type^ t) : CBatesModel(CBatesDetJumpModel::typeid)
{
#ifdef HANDLE
	_phBatesDetJumpModel = NULL;
#endif
	if (!t->IsSubclassOf(CBatesDetJumpModel::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Equity::CBatesDetJumpModel::CBatesDetJumpModel (QuantLib::Handle<QuantLib::BatesDetJumpModel>& childNative, Object^ owner)  : CBatesModel(CBatesDetJumpModel::typeid)
{
	_phBatesDetJumpModel = &childNative;
	_ppBatesDetJumpModel = &static_cast<boost::shared_ptr<QuantLib::BatesDetJumpModel>>(childNative.currentLink());
    _ppBatesModel = new boost::shared_ptr<QuantLib::BatesModel> (boost::dynamic_pointer_cast<QuantLib::BatesModel> (*_ppBatesDetJumpModel));
    _BatesDetJumpModelOwner = owner;
}
Cephei::QL::Models::Equity::CBatesDetJumpModel::CBatesDetJumpModel (QuantLib::Handle<QuantLib::BatesDetJumpModel> childNative)  : CBatesModel(CBatesDetJumpModel::typeid)
{
	_phBatesDetJumpModel = &childNative;
	_ppBatesDetJumpModel = &static_cast<boost::shared_ptr<QuantLib::BatesDetJumpModel>>(childNative.currentLink());
    _ppBatesModel = new boost::shared_ptr<QuantLib::BatesModel> (boost::dynamic_pointer_cast<QuantLib::BatesModel> (*_ppBatesDetJumpModel));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Equity::CBatesDetJumpModel::CBatesDetJumpModel (QuantLib::BatesDetJumpModel childNative)  : CBatesModel(CBatesDetJumpModel::typeid)
{
#ifdef HANDLE
	_phBatesDetJumpModel = NULL;
#endif
	_ppBatesDetJumpModel = new boost::shared_ptr<QuantLib::BatesDetJumpModel> (new QuantLib::BatesDetJumpModel (childNative));
    _ppBatesModel = new boost::shared_ptr<QuantLib::BatesModel> (boost::dynamic_pointer_cast<QuantLib::BatesModel> (*_ppBatesDetJumpModel));
}
#endif

Cephei::QL::Models::Equity::CBatesDetJumpModel::~CBatesDetJumpModel ()
{
    if (_ppBatesDetJumpModel != NULL)
    {
	    delete _ppBatesDetJumpModel;
        _ppBatesDetJumpModel = NULL;
    }
}
Cephei::QL::Models::Equity::CBatesDetJumpModel::!CBatesDetJumpModel ()
{
    if (_ppBatesDetJumpModel != NULL)
    {
	    delete _ppBatesDetJumpModel;
    }
}
QuantLib::BatesDetJumpModel& Cephei::QL::Models::Equity::CBatesDetJumpModel::GetReference ()
{
    if (_ppBatesDetJumpModel == NULL) throw gcnew NativeNullException ();
	return **_ppBatesDetJumpModel;
}
boost::shared_ptr<QuantLib::BatesDetJumpModel>& Cephei::QL::Models::Equity::CBatesDetJumpModel::GetShared ()
{
    if (_ppBatesDetJumpModel == NULL) throw gcnew NativeNullException ();
	return *_ppBatesDetJumpModel;
}
QuantLib::BatesDetJumpModel* Cephei::QL::Models::Equity::CBatesDetJumpModel::GetPointer ()
{
    if (_ppBatesDetJumpModel == NULL) throw gcnew NativeNullException ();
	return &**_ppBatesDetJumpModel;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BatesDetJumpModel>& Cephei::QL::Models::Equity::CBatesDetJumpModel::GetHandle ()
{
	if (_phBatesDetJumpModel == NULL)
	{
		_phBatesDetJumpModel = new Handle<QuantLib::BatesDetJumpModel> (*_ppBatesDetJumpModel);
	}
	return *_phBatesDetJumpModel;
}
#endif
bool Cephei::QL::Models::Equity::CBatesDetJumpModel::HasNative () 
{
	return (_ppBatesDetJumpModel != NULL);
}

Double Cephei::QL::Models::Equity::CBatesDetJumpModel::KappaLambda::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBatesDetJumpModel)->kappaLambda ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Equity::CBatesDetJumpModel::ThetaLambda::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBatesDetJumpModel)->thetaLambda ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Equity::IBatesDetJumpModel^ Cephei::QL::Models::Equity::CBatesDetJumpModel_Factory::Create (Cephei::QL::Processes::IHestonProcess^ process, Microsoft::FSharp::Core::FSharpOption<Double>^ lambda, Microsoft::FSharp::Core::FSharpOption<Double>^ nu, Microsoft::FSharp::Core::FSharpOption<Double>^ delta, Microsoft::FSharp::Core::FSharpOption<Double>^ kappaLambda, Microsoft::FSharp::Core::FSharpOption<Double>^ thetaLambda)
{
    return gcnew CBatesDetJumpModel ( process,  lambda,  nu,  delta,  kappaLambda,  thetaLambda);
}
